'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class Snac0407
    Inherits Snac

    Public Sub New()
        MyBase.New(&H4, &H7)
    End Sub

    Public Overrides Function CalculateDataSize() As Integer

    End Function

    Private _CookieID As Decimal
    Public Property CookieID() As Decimal
        Get
            Return _CookieID
        End Get
        Set(ByVal value As Decimal)
            _CookieID = value
        End Set
    End Property

    Private _Channel As MessageChannel
    Public Property Channel() As MessageChannel
        Get
            Return _Channel
        End Get
        Set(ByVal value As MessageChannel)
            _Channel = value
        End Set
    End Property

    Private _ScreenName As String
    Public Property ScreenName() As String
        Get
            Return _ScreenName
        End Get
        Set(ByVal value As String)
            _ScreenName = value
        End Set
    End Property

    Private _SenderWarningLevel As Integer
    Public ReadOnly Property SenderWarningLevel() As Integer
        Get
            Return _SenderWarningLevel
        End Get
    End Property


    Private _AutoResponseFlag As Boolean
    Public ReadOnly Property AutoResponseFlag() As Boolean
        Get
            Return _AutoResponseFlag
        End Get
    End Property

    Private _UserClass As New TlvUserClass
    Public ReadOnly Property UserClass() As TlvUserClass
        Get
            Return _UserClass
        End Get
    End Property

    Private _UserStatus As New TlvUserStatus
    Public ReadOnly Property UserStatus() As TlvUserStatus
        Get
            Return _UserStatus
        End Get
    End Property

    Private _ClientIdleTime As New TlvClientIdleTime
    Public ReadOnly Property ClientIdleTime() As TlvClientIdleTime
        Get
            Return _ClientIdleTime
        End Get
    End Property

    Private _AccountCreationTime As New TlvAccountCreationTime
    Public ReadOnly Property AccountCreationTime() As TlvAccountCreationTime
        Get
            Return _AccountCreationTime
        End Get
    End Property

    Private _MessageData As New TlvMessageData
    Public ReadOnly Property MessageData() As TlvMessageData
        Get
            Return _MessageData
        End Get
    End Property

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Throw New NotImplementedException
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Snac.SizeFixPart

        Dim desc As TlvDescriptor

        _CookieID = ByteConverter.ToUInt64(data.GetRange(index, 8))
        index += 8

        _Channel = DirectCast(CInt(ByteConverter.ToUInt16(data.GetRange(index, 2))), MessageChannel)
        index += 2

        _ScreenName = ByteConverter.ToStringFromByteIndex(index, data)
        index += 1 + _ScreenName.Length

        _SenderWarningLevel = ByteConverter.ToUInt16(data.GetRange(index, 2))
        index += 2

        Dim innerTlvCount As Integer
        Dim innerTlvIndex As Integer

        innerTlvCount = ByteConverter.ToUInt16(data.GetRange(index, 2))
        index += 2

        Do While innerTlvIndex < innerTlvCount
            desc = TlvDescriptor.GetDescriptor(index, data)

            Select Case desc.TypeId
                Case &H1
                    _UserClass.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &H6
                    _UserStatus.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &HF
                    _ClientIdleTime.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &H3
                    _AccountCreationTime.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &H4
                    _AutoResponseFlag = True
            End Select

            index += desc.TotalSize
            innerTlvIndex += 1
        Loop

        desc = TlvDescriptor.GetDescriptor(index, data)

        _MessageData.Deserialize(data.GetRange(index, desc.TotalSize))
        index += _MessageData.TotalSize

        Do While index + 4 <= data.Count
            desc = TlvDescriptor.GetDescriptor(index, data)

            index += desc.TotalSize
        Loop

        Me.SetTotalSize(index)
    End Sub
End Class

Public Class TlvAccountCreationTime
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H5)
    End Sub

    Private _AccountCreationTime As DateTime
    Public Property AccountCreationTime() As DateTime
        Get
            Return _AccountCreationTime
        End Get
        Set(ByVal value As DateTime)
            _AccountCreationTime = value
        End Set
    End Property

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytesForUInt32Date(_AccountCreationTime))

        Return data
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Tlv.SizeFixPart

        _AccountCreationTime = ByteConverter.ToDateTimeFromUInt32FileStamp(data.GetRange(index, 4))
    End Sub

    Public Overrides Function CalculateDataSize() As Integer
        Return 4
    End Function
End Class